package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import bot.ChatBot;

public class ChannelTab extends JPanel {
	ChatBot bot;
	JTextField inputField;
	TextArea textArea;
	JScrollPane scroll;
	JButton sendButton;
	String channel;

	public ChannelTab(String channel, ChatBot bot) {
		this.channel = channel;
		this.setName(channel);
		this.bot = bot;
		textArea = new TextArea();
		inputField = new JTextField();
		sendButton = new JButton("Send");
		createView();

		inputField.addKeyListener(new SubmitListener());
	}

	public void createView() {
		this.setLayout(new BorderLayout());
		scroll = new JScrollPane(textArea);
		scroll.setHorizontalScrollBar(null);
		this.add(scroll, BorderLayout.CENTER);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(inputField, BorderLayout.CENTER);
		panel.add(sendButton, BorderLayout.EAST);
		this.add(panel, BorderLayout.SOUTH);

		sendButton.addActionListener(new SendButtonActionListener());
	}

	public void setBot(ChatBot bot) {
		this.bot = bot;
	}

	public void addText(String user, String message) {
		final JScrollBar vbar = scroll.getVerticalScrollBar();
		// is the scroll bar at the bottom?
		boolean end = vbar.getMaximum() == vbar.getValue()
				+ vbar.getVisibleAmount();

		// append some new text to the text area
		// (or do something else that increases the contents of the JScrollPane)
		textArea.addText(user, message);

		// if scroll bar already was at the bottom we schedule
		// a new scroll event to again scroll to the bottom
		if (end) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					EventQueue.invokeLater(new Runnable() {
						public void run() {
							vbar.setValue(vbar.getMaximum());
						}
					});
				}
			});
		}
	}

	public void sendMessage() {
		try {
			String message = inputField.getText();
			if (!message.equals("")) {
				if (message.startsWith("/")) {
					if (message.startsWith("/me")) {
						bot.sendAction(channel, message.substring(3));
					} else {
						bot.sendRawLine(message);
					}
				} else {
					bot.sendMessage(channel, message, true);
					addText(bot.getNick(), message);
				}
			}
		} catch (Exception e) {
			addText("System", "bericht kon niet worden verzonden");
		} finally {
			inputField.setText("");
		}
	}

	public class SendButtonActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			sendMessage();
		}

	}

	public class SubmitListener implements KeyListener {

		@Override
		public void keyPressed(KeyEvent arg0) {
			if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
				sendMessage();
			}
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
		}
	}
}
